/*
 *@# AdDataLink Created on 2004-7-28
 *
 *
 * Copyright: Bluejay Networks Inc. & Inquiry Management Systems Ltd.  2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 */
package com.ims.iaa.AD;

import java.io.Serializable;
import java.net.URL;
import java.sql.Timestamp;
import java.util.List;
/**
 * This class dsscribe the the common character of link resource such as image or flash,
 * some special function will implement in subclass,
 * the FlashLink class and ImgLink class extend it.
 *  
 * @see ImgLink
 * @see FlashLink
 * 
 * @author ShengMeng Bai
 * @version 1.0
 */
public abstract class AdDataLink implements Serializable {
	
	//connection status, it used in subclass
	static public final int CONN_NEVER   = 0;
	static public final int CONN_SUCCESS = 1;
	static public final int CONN_BAD_URL = 2;
	static public final int CONN_FAILURE = 3;
	/** the advertisement type, if the AD. is show with image,the type is "IMG", 
	 * if the AD. show with flash, the type is "FLASH", else if  the AD. show with applet, 
	 * the type is "APPLET", but if they are popup window, add "/POPUP" after the end of type;
	 */
	static public final String ADTYPE_IMAGE = "IMG";
	static public final String ADTYPE_FLASH = "FLASH";
	static public final String ADTYPE_APPLET = "APPLET";
	static public final String ADTYPE_AGENCY = "AGENCY";
	static public final String ADTYPE_POPUP_IMAGE = "IMG/POPUP";
	static public final String ADTYPE_POPUP_FLASH = "FLASH/POPUP";
	static public final String ADTYPE_POPUP_APPLET = "APPLET/POPUP";
	/** The possibility of a image(or flash) is advertisement
	 * usually, the flash is advertisement, the possibility is
	 * a percent number, if m_iPossibility of a image is 75, it's means to
	 * This image is the advertisement possibility reaches 75%.
	 * it range between 0 and 100.
	 */
	private int m_iPossibility;
	/* Record some information about the image or flash, such as
	 * "the image is too small", "it is a agency".
	 */ 
	private String m_sReason;
	/* the ALT field of the <IMG> tag */
	private String m_sAdTitle = "None";
	/**
	 * If the image(or flash) is a agency, record the agency information,
	 * if not, it should be null.
	 * if m_objAdAgency unequal null, means to the advertisement is a agency
	 * advertisement, the image(or flash) refer to a agency URL.
	 * @see AdAgency
	 */
	private AdAgency  m_objAdAgency;
	/*
	 * Reserved for future use, Not used in this release.
	 */
	private String m_sLocation = "Unknown";	
	/**
	 * if the AD data show in a popup window, we will set 
	 * the m_sPopupParent = (the URL String which pop up window),
	 * and set m_bIsPopup = true.
	 * 
	 * #see setPopupParentURLStr().
	 */
	private boolean m_bIsPopup = false;
	private String m_sPopupParent = null;
	
	/**
	 * Some impression need re-connect, 
	 * m_iImpressionID record the old ImpressionID 
	 * m_iOldImageID record the old ImageID
	 * @see #getOldImpressionID()
	 * @see #setOldImpressionID(int)
	 * @see #getOldImageID()
	 * @see #setOldImageID(int))
	 */
	private int m_iOldImpressionID = -1;
	private int m_iOldImageID = -1;
	
	/**
	 * Get the advertise type.
	 * It is implemented in subclass 
	 * @return ADTYPE_IMAGE, ADTYPE_FLASH, ADTYPE_APPLET, ADTYPE_POPUP_IMAGE, ADTYPE_POPUP_FLASH or ADTYPE_POPUP_APPLET
	 */
	abstract public String getAdType();
	/**
	 * Get the AdData object(in fact, it should be AdData subclass object).
	 * It is implemented in subclass 
	 * @see AdData
	 * @see AdFlash
	 * @see AdImage
	 */
	abstract public AdData getAdData();
	/**
	 * Get the AdData height
	 * It is implemented in subclass 
	 * @return Height in pixels
	 */
	abstract public int getHeight();
	/**
	 * Set the AdData height
	 * It is implemented in subclass 
	 * @param iHeight Height in pixels
	 */
	abstract public void setHeight(int iHeight);
	/**
	 * Get the AdData width
	 * It is implemented in subclass 
	 * @return Width in pixels
	 */
	abstract public int getWidth();
	/**
	 * Set the AdData width
	 * It is implemented in subclass 
	 * @param iWidth width in pixels
	 */
	abstract public void setWidth(int iWidth);
	/**
	 * Set the User Agent string which used during the search process
	 * @param agent
	 */
	abstract public void setUserAgent(String agent);
	/**
	 * Get the source URL of the image(or flash).
	 * @return 
	 */
	abstract public URL getSrcURL();
	/**
	 * Get the source URL string of the image(or flash).
	 * @return 
	 */
	abstract public String getSrcURLStr();
	/**
	 * Get the host URL of the image(or flash) source.
	 * @return
	 */
	abstract public String getAdDataSite();
	/**
	 * Get the web URL which this image(or flash) is showed.
	 * @return
	 */
	abstract public URL getContainerURL();
	/**
	 * Get the URL object which the image(or flash) refering to.
	 * if the data is image, it is the href property of A tag 
	 * which contain IMG, such as <A href=... ><IMG src=...>.
	 * if the data is flash, usually cann't get this URL.
	 * @return  if the resource is flash, will return null.
	 */
	abstract public URL getTargetURL();
	/**
	 * Get the URL string which the image(or flash) refering to.
	 * see #getTargetURL to get more detail.
	 * @return if the resource is flash, will return null.
	 */
	abstract public String getTargetURLStr();
	/**
	 * Usually, the image TargetURL isn't real target,
	 * we can connect to the real targetURL, get real target url.
	 * the real target URL will be get in connectToResource method in subclass
	 * more detail see subclass.
	 * 
	 * @return if the resource is flash, will return null.
	 */
	abstract public String getRealTargetURLStr();
	/**
	 * Get the host URL string of real target URL.
	 * @return  if the resource is flash, will return null.
	 */
	abstract public String getAdvertiser();
	/**
	 * Get timestamp of connecting to resource
	 * @return
	 */
	abstract public Timestamp getCollectTime();
	/**
	 * connect to the URL, and parse the resource.
	 * @param lstAgency, the ims.iaa.AD.AdAgency object list.  
	 * 
	 * @return true, if the resouce need save, else false.
	 */
	abstract public boolean parse(List lstAgency,List lstAdSize);
	/**
	 * get the ad data connection state.
	 */
	abstract public boolean isConnectSuccessful();
	/**
	 * Increase the possibility of advertisement.
	 * see m_iPossibility to get more detail.
	 * @param iPossibility
	 */
	protected void incPossibility( int iPossibility ) {
		m_iPossibility += iPossibility;
		if (m_iPossibility > 100) m_iPossibility = 100;
	}
	/**
	 * Decrease the possibility of advertisement.
	 * see m_iPossibility to get more detail.
	 * @param iPossibility
	 */
	protected void decPossibility( int iPossibility ) {
		m_iPossibility -= iPossibility;
		if (m_iPossibility < 0) m_iPossibility = 0;
	}	
	/**
	 * Get the possibility of advertisement.
	 * see m_iPossibility to get more detail.
	 */
	public int getPossibility()  {
		return m_iPossibility;
	}
	/**
	 * Get the agency information of the advertisement.
	 * see m_objAdAgency to get more detail.
	 * @return
	 */
	public AdAgency getAdAgency()  {
		return m_objAdAgency;
	}
	/**
	 * Set the agency information of the advertisement.
	 * see m_objAdAgency to get more detail.
	 * @param objAdAgency
	 */
	protected void setAdAgency(AdAgency objAdAgency){
		m_objAdAgency = objAdAgency;		
	}

	/**
	 * @see #m_sLocation
	 */
	public String getLocation()  {
		return m_sLocation;
	}
	/**
	 * @see #m_sLocation
	 */
	protected void setLocation(String loc)  {
		m_sLocation = loc;
	}

	public void addReason( String value ) {
		if (m_sReason != null ) {
			m_sReason += value ;
		} else  {
			m_sReason = value;
		}
	}

	public String getReason()  {
		return m_sReason;
	}
	
	public void setAdTitle(String title)    {
		m_sAdTitle = title;
	}
	public String getAdTitle()	 {
		return m_sAdTitle;
	}
	/**
	 * If the AdDataLink contain in a popup window, you must
	 * set the parent of the popup window,
	 * in this method, we will set m_bIsPopup = true, it means to
	 * if you call this method, the AdData should be showed in popup window.
	 * 
	 * @param sParent the URL string of parent of pop up window 
	 */
	public void setPopupParentURLStr(String sParent){
		m_bIsPopup = true;
		m_sPopupParent = sParent;
	}
	/**
	 * see #setPopupParentURLStr
	 * @return
	 */
	public boolean isPopupData(){
		return m_bIsPopup;
	}
	/**
	 * see #setPopupParentURLStr
	 * @return
	 */
	public String getPopupParentURLStr(){
		return m_sPopupParent;
	}
	/**
	 * This method will be overide in AdAppletLink.
	 * it always return null in here.
	 * 
	 * @see AdAppletLink
	 * @return null
	 */
	public String getAppletParamStr(){
		return null;
	}
	
	/**
	 * Some impression need re-connect, 
	 * m_iImpressionID record the old ImpressionID 
	 * m_iOldImageID record the old ImageID
	 * @see #m_iOldImpressionID
	 * @see #m_iOldImageID
	 */
	public int getOldImpressionID(){
		return m_iOldImpressionID;
	}
	/**
	 * Some impression need re-connect, 
	 * m_iImpressionID record the old ImpressionID 
	 * m_iOldImageID record the old ImageID
	 * @see #m_iOldImpressionID
	 * @see #m_iOldImageID
	 */
	public int getOldImageID(){
		return m_iOldImageID;
	}
	/**
	 * Some impression need re-connect, 
	 * m_iImpressionID record the old ImpressionID 
	 * m_iOldImageID record the old ImageID
	 * @see #m_iOldImpressionID
	 * @see #m_iOldImageID
	 */
	public void setOldImpressionID(int iID){
		m_iOldImpressionID = iID;
	}
	/**
	 * Some impression need re-connect, 
	 * m_iImpressionID record the old ImpressionID 
	 * m_iOldImageID record the old ImageID
	 * @see #m_iOldImpressionID
	 * @see #m_iOldImageID
	 */
	public void setOldImageID(int iID){
		m_iOldImageID = iID;
	}
	
	public boolean equals(Object obj){
		if(obj instanceof AdDataLink){
			int iID = ((AdDataLink)obj).getOldImpressionID();
			if(m_iOldImpressionID == iID){
				return true;
			}
		}
		return false;
	}
	/**
	 * translate connection status to a string.
	 * @param status should be CONN_NEVER, CONN_SUCCESS, CONN_BAD_URL or CONN_FAILURE.
	 * @return the status with string
	 */
	public static String statusToString( int status ) {
		String statusMsg = null;
		switch ( status )  {
			case CONN_NEVER:
				statusMsg = "not connect"; break;
			case CONN_SUCCESS:
				statusMsg = "success"; break;
			case CONN_BAD_URL:
				statusMsg = "bad url"; break;
			case CONN_FAILURE:
				statusMsg = "failure"; break;
			default:
				statusMsg = "unknown staus code"; break;
		}
		return statusMsg;
	}
	public void setPublisherURL(URL url){
	    m_publisherURL = url;
	}
	public URL getPublisherURL(){
	    if(m_publisherURL == null){
	        throw new RuntimeException("YOu must set the publisher URL at first.");
	    }
	    return m_publisherURL;
	}
	public String getPublisher(){
	    return getPublisherURL().getHost();
	}
	private URL m_publisherURL;
	
	//++changsheng for crawler depth
	private int m_depth ;
	public int getDepth()
	{
	    return m_depth ;
	}
	public void setDepth(int depth)
	{
	    this.m_depth = depth ;
	} 
    private String originalImgURL = null ;
    public void setOriginalImgURL(String originalImgURL){
        this.originalImgURL = originalImgURL ;
    }
    public String getOriginalImgURL(){
        return originalImgURL ;
    }
}
